Ext.regController('Navigator', {
    anims:{
        forward: {type: 'slide', duration: 400, direction: 'left'},
        back: {type: 'slide', duration: 400, direction: 'right'}
    },    
    init: function() {
    },
    entryChosen: function(options) {
    	options.ownerView.hide(this.anims.back);
        var listConfig = createEntitiesList({tableName: options.tableName});
        this.currentView = new EntitiesListView({itemsConfig: listConfig, title: options.labelTo, ownerView: options.ownerView});
        this.currentView.show(this.anims.forward);
        this.currentView.down('list').store.load();       
    },
    onEntityChoose: function(options) {
    	options.ownerView.hide(this.anims.back);
    	this.currentView = new EntityView({fieldsetConfig: createFiedSet(options.tableName), entityRecord: options.entityRecord, ownerView: options.ownerView});
    	this.currentView.show(this.anims.forward);
    },
    back: function(options) {
    	this.currentView.readyDestroy = true;
    	this.currentView.hide(this.anims.forward);
    	this.currentView.ownerView.show(this.anims.back);
    	this.currentView = this.currentView.ownerView; 
    },
    
    
    
    
    
    toggleToolbar: function(v, state){
        if (state=='hide' && !v.toolbar.isVisible()) v.toolbar.show();
        if ((me = v.whereOpened) && me.toolbar) {
            me.toolbar[state]();
            me.doComponentLayout();
        }
    },
    
    /*back: function(options) {
        (me = options.view).whereOpened.setActiveItem(0,Ext.apply(this.anims.back,{
            after: function(){
                !me.list || me.list.scroller.setOffset({y:0});
                me.setActiveItem(0,false);
            }
        }));
        
        this.toggleToolbar(me, 'show')
    },*/
    
    
    recordChosen: function(options) {
        var view = options.view,
            me = view.detailViewer
        ;
        
        me.filter.value = options.record.get('id');
        
        me.store.load({
            scope: this,
            callback: function(){
                this.toggleToolbar(me, 'hide');        
                view.setActiveItem(me, Ext.apply(this.anims.forward,{
                    after: function() {
                        me.setActiveItem(0);
                        !me.list || me.list.scroller.setOffset({y:0});
                    }
                }));
            }
        })
        
    },

    recordViewerButton: function(options) {
        var view = options.view,
            me = view.detailViewer || (view.detailViewer = new TableViewer({ whereOpened: view }));
        
        this.recordViewerSetup(
            me.detailViewer || (me.detailViewer = new RecordViewer({ whereOpened: me })),
            options.tableName
        );
        

        (me.list.store = view.record[options.tableTo]()).load({
            scope: this,
            callback: function(){
                me.list.refresh();
                me.toolbar.setTitle(options.labelTo);
                this.toggleToolbar(me,'hide');
                view.setActiveItem(me, Ext.apply(this.anims.forward,{
                    after: function() {
                        me.setActiveItem(0);
                        !me.list || me.list.scroller.setOffset({y:0});
                    }
                }));
            }
        })
        
    },

    recordViewerSetup: function(viewer, tableName){
        var me=viewer,
            fieldSet = me.fieldSet,
            table = dataSync.tables[tableName]
        ;
        
        fieldSet.removeAll();
        
        me.tableName=tableName;
        
        me.filter = new Ext.util.Filter({
            property: 'id',
            value: 0
        });
        
        Ext.each( dataSync.tables[tableName].columns, function(column) {
            var label = dbSchema.labels[column.name];
            
            if (label && !(column.name == 'name')){
                if (column.fk)
                    
                    fieldSet.add( new Ext.form.Select({
                        name : column.name,
                        "label": label,
                        valueField : 'id',
                        displayField : 'name',
                        
                        store :  new Ext.data.Store({
                            model : column.fk.parent.name,
                            autoLoad : true,
                            
                            proxy: {
                                type: 'sql',
                                engine: dataSync,
                                tableName: column.fk.parent.name
                            }
                        })
                    }));
                    
                else
                    fieldSet.add( new Ext.form.Field ({
                        xtype: 'textfield',
                        useMask: true,
                        name : column.name,
                        "label": label
                    }))
            }
        });
        
        me.buttonList.removeAll();
     
        Ext.each (dataSync.tables[tableName].deps, function(dep){
            if (dep.child.label) me.buttonList.add({
                
                xtype: 'button',
                text: dep.child.label,
                ui: 'normal',
                
                handler: function(){
                    Ext.dispatch({
                        controller: 'Navigator',
                        action: 'recordViewerButton',
                        tableName: dep.child.name,
                        tableTo: dep.name,
                        labelTo: dep.child.label,
                        view: viewer
                    })
                }
                
            })
        });
        
        fieldSet.doComponentLayout();
        me.buttonList.doComponentLayout();
        //me.doLayout();
        
        
        if (fieldSet.items.getCount() == 0)
            fieldSet.addCls('no-items')
        else
            fieldSet.removeCls('no-items');
        
        me.store = new Ext.data.Store({
            model: tableName,
            proxy: {type: 'sql', "tableName": tableName, engine: dataSync},
            remoteFilter: true,
            remoteSort: true,
            autoLoad: false,
            
            filters: [
                me.filter
            ],
            
            listeners:{
                load:{
                    fn: function(records, operation, success, e) {
                        
                        me.down('form').load( me.record = records.first() );
                        me.toolbar.setTitle( me.record.get('name') );
                        
                    }
                }
            }
            
        })
        
    }
    
})